/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#ifndef TRY_GRAPHICSMANAGER_H
#define TRY_GRAPHICSMANAGER_H

#include "rendercontext.h"
#include <listenable.h>
#include <singleton.h>

#define graphics GraphicsManager::instance()

#define TRY_DEFAULT_SEGMENT_COUNT 48

namespace Try
{
    class GraphicsManager : public Singleton<GraphicsManager>, public Listenable<RenderListener>
    {
    private:
        struct VertexDef
        {
            Vector3 pos;
            Color color;

            VertexDef(const Vector3& pos, const Color& color)
                : pos(pos), color(color) {}
        };

        std::vector<VertexDef> m_lineVertices;
        std::vector<int> m_lineIndices;
        int m_lineIndex;

    protected:
        RenderContextList m_renderContexts;

    public:
        GraphicsManager(RenderContext* rc = 0);
        virtual ~GraphicsManager();

        void registerRenderContext(RenderContext* rc);
        void unregisterRenderContext(RenderContext* rc);

        void render(RenderContext* rc, double dt = 0);
        void renderAll(double dt = 0);

        int renderContextCount() const;
        bool hasRenderContext(RenderContext* rc) const;

        void drawLine(const Vector3& start, const Vector3& end, const Color& color = Colors::White);
        void drawCircle(const Vector3& center, float radius, int segment_count = TRY_DEFAULT_SEGMENT_COUNT, const Color& color = Colors::White);
        void drawQuad(const Vector3& center, const float width, float height, const Color& color = Colors::White);
        void drawGrid(const Vector3& center, const Color& color = Colors::White, float cell_size = 20, int numcols = 30, int numrows = 30);
        void drawBox(const Vector3& center, const Vector3& size, const Color& color = Colors::White);
        void drawCone(const Vector3& center, float radius, float length, int segment_count = TRY_DEFAULT_SEGMENT_COUNT, const Color& color = Colors::White);
        void drawCylinder(const Vector3& center, float radius, float length, int segment_count = TRY_DEFAULT_SEGMENT_COUNT, const Color& color = Colors::White);

    protected:
        void renderPrimitives(RenderContext* rc);
        void finalizeRender();
        void addLineVertex(const Vector3& vertex, const Color& color);
        void addLineIndices(int index1, int index2);
    };
}

#endif // TRY_GRAPHICSMANAGER_H
